package com.phrp.service.impl;

import com.phrp.entity.dto.RentalApplyDataDTO;
import com.phrp.entity.vo.ApplyRecordSearchDataVo;
import com.phrp.exception.CustomizationException;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.phrp.common.ConstantEnum;
import com.phrp.entity.HouseRecord;
import com.phrp.dao.HouseRecordDao;
import com.phrp.entity.dto.LeaseApplyDataDTO;
import com.phrp.entity.vo.PageData;
import com.phrp.service.HouseRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ResponseResult;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-04-06
 */
@Service
public class HouseRecordserviceImpl extends ServiceImpl<HouseRecordDao, HouseRecord> implements HouseRecordService {

    @Autowired
    private HouseRecordDao houseRecordDao;

    /**
     * 根据条件搜索租房申请信息
     *
     * @param searchData
     * @return
     */
    @Override
    public ResponseResult searchRentalApply(ApplyRecordSearchDataVo searchData) {

        /*
            这里出现了很多问题，首先是数据的接收类型，类型错误就会导致400错误，其次就是时间的比较
            在sql中，从数据库取出的时间会变成字符串类型，比较的话就需要传输一个字符串到sql中，
            不能传输时间类型，因为一旦传输时间类型就会报错（字符串和时间的比较）
         */

        if (searchData.getSearchApplyTime().size() > 0) {
            // 解析并转换第一个时间
            LocalDateTime startDateTime = LocalDateTime.parse(searchData.getSearchApplyTime().get(0), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setATimeStart(startDateTime.toLocalDate().toString());

            // 解析并转换第二个时间
            LocalDateTime endDateTime = LocalDateTime.parse(searchData.getSearchApplyTime().get(1), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setATimeEnd(endDateTime.toLocalDate().toString());
        }

        if (searchData.getSearchHandleTime().size() > 0) {
            // 解析并转换第一个时间
            LocalDateTime startDateTime = LocalDateTime.parse(searchData.getSearchHandleTime().get(0), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setHTimeStart(startDateTime.toLocalDate().toString());

            // 解析并转换第二个时间
            LocalDateTime endDateTime = LocalDateTime.parse(searchData.getSearchHandleTime().get(1), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setHTimeEnd(endDateTime.toLocalDate().toString());
        }

        searchData.setStartPage((searchData.getCurrentPage() - 1) * searchData.getPageSize());
        searchData.setApplyType(ConstantEnum.RENTALHOUSE.getCode());

        LinkedList<RentalApplyDataDTO> applyDatas = houseRecordDao.selectBySearchData(searchData);
        Integer total = houseRecordDao.getRentalTotalNum(searchData);

        PageData<LinkedList<RentalApplyDataDTO>> linkedListPageData = new PageData<>();
        linkedListPageData.setTotalNumber(total);
        linkedListPageData.setData(applyDatas);

        return new ResponseResult(ReturnInfoEnum.SUCCESS, linkedListPageData);
    }

    /**
     * 修改申请状态
     * 修改处理结果
     * @param id,type
     * @return
     */
    @Override
    public ResponseResult changeApplyResult(String id, String type) {
        UpdateWrapper<HouseRecord> update = new UpdateWrapper<>();
        update.eq("hr_uuid", id)
                .set("handle_result", type)
                .set("handle_time", LocalDate.now());

        int i = houseRecordDao.update(null, update);
        if (i != 1) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 获取申请信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult getRentalApplyData(String id) {
        RentalApplyDataDTO applyData = houseRecordDao.selectByHrUuid(id);
        if(applyData == null){
            // 数据出现错误，获取数据的id是一定存在的，但数据库查询错误
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS,applyData);
    }

    /**
     * 获取退租申请的信息
     * @param searchData
     * @return
     */
    @Override
    public ResponseResult searchLeaseApply(ApplyRecordSearchDataVo searchData) {
        if (searchData.getSearchApplyTime().size() > 0) {
            // 解析并转换第一个时间
            LocalDateTime startDateTime = LocalDateTime.parse(searchData.getSearchApplyTime().get(0), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setATimeStart(startDateTime.toLocalDate().toString());

            // 解析并转换第二个时间
            LocalDateTime endDateTime = LocalDateTime.parse(searchData.getSearchApplyTime().get(1), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setATimeEnd(endDateTime.toLocalDate().toString());
        }

        searchData.setStartPage((searchData.getCurrentPage()-1) * searchData.getPageSize());
        searchData.setApplyType(ConstantEnum.LEASEHOUSE.getCode());

        LinkedList<LeaseApplyDataDTO> datas = houseRecordDao.selectLeaseByData(searchData);

        Integer i = houseRecordDao.getLeaseTotalNum(searchData);

        PageData<LinkedList<LeaseApplyDataDTO>> pageData = new PageData<>();

        pageData.setData(datas);
        pageData.setTotalNumber(i);
        return new ResponseResult(ReturnInfoEnum.SUCCESS,pageData);
    }

    /**
     * 获取退租申请详情
     * @param id
     * @return
     */
    @Override
    public ResponseResult searchLeaseData(String id) {
        LeaseApplyDataDTO data = houseRecordDao.selectLeaseById(id);
        if(data == null){
            // 数据出现错误，获取数据的id是一定存在的，但数据库查询错误
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS,data);
    }
}
